package com.wtb.utils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
	private static DateFormat f1 = new SimpleDateFormat("yyyyMMddHHmmss");
	private static DateFormat f2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * 返回accessToken的创建时间
	 * @return  1338451544000
	 */
	public static String createTime() {
		Date now = new Date();
		return f1.format(now);
	}
	/**
	 * 获取当前系统时间 单位为秒
	 * @return  1338451544
	 */
	public static String systemTime(){
		return String.valueOf(System.currentTimeMillis()/1000);
	}
	
	/**
	 * 将毫秒转换为时间
	 * @param millisns  1338450837046
	 * @return  Thu May 31 15:47:56 CST 2012
	 */
	public static Date toDate(long millins) {
		return new Date(millins);
	}
	
	/**
	 * 将毫秒转换为时间字符串
	 * @param millisns  1338450837046
	 * @return  20120531160544
	 */
	public static String toDateString(long millins) {
		return toDateString(toDate(millins));
	}

	/**
	 * 将时间字符串转换为毫秒
	 * @param dateString  2012-05-31 16:08:36
	 * @return  1338451544000
	 */
	public static long toMillins(String dateString) {
		try {
			Date date = toDate(dateString);
			return date.getTime();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 将时间字符串转换为毫秒
	 * @param dateString  20120531160544
	 * @return  1338451544000
	 */
	public static long toMillins_(String dateString) {
		try {
			Date date = toDate_(dateString);
			return date.getTime();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 将给定格式是时间字符串 转换为统一样式
	 * @param dateString  2012-05-31 16:08:36
	 * @return   20120531161302
	 */
	public static String toDateString(String dateString) {
		return f1.format(toDate(dateString));
	}
	
	/**
	 * 将给定格式是时间字符串 转换为统一样式
	 * @param dateString  20120531161302
	 * @return   2012-05-31 16:08:36
	 */
	public static String _toDateString(String dateString) {
		return f2.format(toDate_(dateString));
	}
	
	/**
	 * 获取时间字符串
	 * @return   2012-05-31 16:08:36
	 */
	public static String getDateString() {
		return f2.format(new Date());
	}
	
	/**
	 * 将给定时间转换为字符串
	 * @param dateString  Thu May 31 16:01:17 CST 2012
	 * @return  20120531154639
	 */
	public static String toDateString(Date date) {
		try {
			return f1.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 将给定字符串转换为时间
	 * @param dateString  2012-05-31 16:08:36
	 * @return  Thu May 31 16:03:12 CST 2012
	 */
	public static Date toDate(String dateString) {
		try {
			return f2.parse(dateString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 将给定字符串转换为时间
	 * @param dateString   20120531160312
	 * @return  Thu May 31 16:03:12 CST 2012
	 */
	public static Date toDate_(String dateString) {
		try {
			return f1.parse(dateString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	/**
	 * 判断人人AccessToken是否过期
	 * @param createKeyDate
	 * @param expire_time
	 * @return
	 */
	public static boolean isValidKey(String creatTime, long expire_time) {
		Date now = new Date();
		try {
			Date createTime_Date = DateUtils.toDate_(creatTime);
			long time = now.getTime()-24*60*60 - createTime_Date.getTime();
			return time > expire_time ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}
